#######################################
# Tables in the appendix
#######################################
library(tidyverse)
library(bucky)
library(ggeffects)
library(stargazer)

setwd() # set working directory to the folder with the replication materials

partylist <- read.csv("party_list.csv")

# Countries with radical right and radical left parties
c_rr <- partylist %>%
  filter(has_seats==1, populist == 1, far_right == 1) %>%
  dplyr::select(cntry) %>% distinct() %>% pull() %>%
  c(.,"United Kingdom") # Add UK (UKIP)

c_rl <- partylist %>%
  filter(has_seats==1, populist == 1, far_left == 1) %>%
  filter(cntry != "United Kingdom") %>% # it's incorrectly included because of Sinn Féin
  dplyr::select(cntry) %>% distinct() %>% pull()

# Load the main dataset
dat <- read.csv("ESS_cleaned.csv") 

# Tables ----------------------------------------------------------------

############################
# Table B5
############################
dat %>%
  filter(cntry %in% c(c_rl, c_rr) ) %>%
  summarise(inc_unfair = weighted.mean(inc_unfair>0, w= dweight, na.rm = T),
            job_self_unfair = weighted.mean(job_self_unfair>5, w= dweight, na.rm = T),
            top_inc_unfair = weighted.mean(top_inc_unfair>0, w= dweight, na.rm = T),
            bttm_inc_unfair = weighted.mean(bttm_inc_unfair>0, w= dweight, na.rm = T),
  ) %>%
  mutate_all(round,2) %>%
  knitr::kable()

dat %>%
  group_by(cntry) %>%
  summarise(inc_unfair = weighted.mean(inc_unfair>0, w= dweight, na.rm = T),
            job_self_unfair = weighted.mean(job_self_unfair>5, w= dweight, na.rm = T),
            top_inc_unfair = weighted.mean(top_inc_unfair>0, w= dweight, na.rm = T),
            bttm_inc_unfair = weighted.mean(bttm_inc_unfair>0, w= dweight, na.rm = T),
  ) %>%
  mutate_at(vars(-cntry),round,2) %>%
  knitr::kable()

##################################################
# Table E.1: Table 1 without any regional controls
##################################################
# Column 1
t1_1 <- lm( inc_unfair ~ RTI + netjobreduction_manu_scaled + netjobreduction_non_manu_scaled + 
              college + female + age + income + union + unemployed + 
              citizen + religion + urban + cntry,
            data = dat, weights = dweight) 
t1_1 <- robustify(t1_1 , cluster = code_2003)

# Column 2
t1_2 <- lm( top_inc_unfair ~ RTI + netjobreduction_manu_scaled + netjobreduction_non_manu_scaled + 
              college + female + age + income + union + unemployed + 
              citizen + religion + urban + cntry,
            data = dat, weights = dweight) 
t1_2 <- robustify(t1_2, cluster = code_2003)

# Column 3
t1_3 <- lm( bttm_inc_unfair ~ RTI + netjobreduction_manu_scaled + netjobreduction_non_manu_scaled + 
              college + female + age + income + union + unemployed +
              citizen + religion + urban + cntry,
            data = dat, weights = dweight) 
t1_3 <- robustify(t1_3, cluster = code_2003)

# Column 4
t1_4 <- lm( job_self_unfair ~ RTI + netjobreduction_manu_scaled + netjobreduction_non_manu_scaled + 
              college + female + age + income + union + unemployed +
              citizen + religion + urban + cntry,
            data = dat, weights = dweight) 
t1_4 <- robustify(t1_4, cluster = code_2003)

# Column 5
t1_5 <- lm( job_other_unfair ~ RTI + netjobreduction_manu_scaled + netjobreduction_non_manu_scaled + 
              college + female + age + income + union + unemployed +
              citizen + religion + urban + cntry,
            data = dat, weights = dweight) 
t1_5 <- robustify(t1_5, cluster = code_2003)

# Combined
stargazer(t1_1,t1_2,t1_3,t1_4,t1_5, type = "text")


####################################################
# Table E.2.a: Table 2 with Binary variables
####################################################

# Column 1
a3_1 <- lm( redist ~ d_inc_unfair + d_top_inc_unfair + d_bttm_inc_unfair + 
              college + female + age + income + union + unemployed + 
              citizen + religion + urban + cntry,
            data = dat, weights = dweight) 
a3_1 <- robustify(a3_1 , cluster = cntry)

# Column 2 
a3_2 <- lm( redist ~ d_job_self_unfair + d_job_other_unfair +
              college + female + age + income + union + unemployed + 
              citizen + religion + urban + cntry,
            data = dat, weights = dweight) 
a3_2 <- robustify(a3_2 , cluster = cntry)

# Column 3
a3_3 <- lm( imm_culture ~ d_inc_unfair + d_top_inc_unfair + d_bttm_inc_unfair + 
              college + female + age + income + union + unemployed + 
              citizen + religion + urban + cntry,
            data = dat, weights = dweight) 
a3_3 <- robustify(a3_3 , cluster = cntry)

# Column 4
a3_4 <- lm( imm_culture ~ d_job_self_unfair + d_job_other_unfair +
              college + female + age + income + union + unemployed + 
              citizen + religion + urban + cntry,
            data = dat, weights = dweight) 
a3_4 <- robustify(a3_4 , cluster = cntry)

# Combined
stargazer(a3_1,a3_2,a3_3,a3_4, type = "text")

################################################
# Table E.2.b: Table 3 with Binary variables
################################################

# Column 1
a4_1 <- glm( far_left ~ d_inc_unfair + d_top_inc_unfair + d_bttm_inc_unfair + 
               college + female + age + income + union + unemployed + 
               citizen + religion + urban + cntry,
             data = dat  %>% filter(cntry %in% c_rl),
             weights = dweight, family=binomial(link=logit)) 
a4_1 <- robustify(a4_1, cluster = cntry)

# Column 2
a4_2 <- glm( far_left ~ d_job_self_unfair + d_job_other_unfair +
               college + female + age + income + union + unemployed + 
               citizen + religion + urban + cntry,
             data = dat %>% filter(cntry %in% c_rl),
             weights = dweight, family=binomial(link=logit)) 
a4_2 <- robustify(a4_2 , cluster = cntry)

# Column 3
a4_3 <- glm( far_right ~ d_inc_unfair + d_top_inc_unfair + d_bttm_inc_unfair + 
               college + female + age + income + union + unemployed + 
               citizen + religion + urban + cntry,
             data = dat %>% filter(cntry %in% c_rr),
             weights = dweight, family=binomial(link=logit)) 
a4_3 <- robustify(a4_3, cluster = cntry)

# Column 4
a4_4 <- glm( far_right ~ d_job_self_unfair + d_job_other_unfair +
               college + female + age + income + union + unemployed + 
               citizen + religion + urban + cntry,
             data = dat %>% filter(cntry %in% c_rr),
             weights = dweight, family=binomial(link=logit)) 
a4_4 <- robustify(a4_4 , cluster = cntry)

# Combined
stargazer(a4_1,a4_2,a4_3,a4_4, type = "text")


#####################################################################
# Table E.3: Table 2, 3: Excluding respondents w/ "self-income unfairly high"
###################################################################### 
# Table 2 column 1
a5_1 <- lm( redist ~ inc_unfair + top_inc_unfair + bttm_inc_unfair + 
              college + female + age + income + union + unemployed + 
              citizen + religion + urban + cntry,
            data = dat %>% filter(inc_unfair>=0), weights = dweight) 
a5_1 <- robustify(a5_1 , cluster = cntry)

# Table 2 column 3
a5_2 <- lm( imm_culture ~ inc_unfair + top_inc_unfair + bttm_inc_unfair + 
              college + female + age + income + union + unemployed + 
              citizen + religion + urban + cntry,
            data = dat %>% filter(inc_unfair>=0), weights = dweight) 
a5_2 <- robustify(a5_2 , cluster = cntry)

# Table 3 column 1
a5_3 <- glm( far_left ~ inc_unfair + top_inc_unfair + bttm_inc_unfair + 
               college + female + age + income + union + unemployed + 
               citizen + religion + urban + cntry,
             data = dat %>% filter(cntry %in% c_rl, inc_unfair>=0),
             weights = dweight, family=binomial(link=logit)) 
a5_3 <- robustify(a5_3 , cluster = cntry)

a5_4 <- glm( far_right ~ inc_unfair + top_inc_unfair + bttm_inc_unfair + 
               college + female + age + income + union + unemployed + 
               citizen + religion + urban + cntry,
             data = dat %>% filter(cntry %in% c_rr, inc_unfair>=0),
             weights = dweight, family=binomial(link=logit)) 
a5_4 <- robustify(a5_4, cluster = cntry)

# Combined
stargazer(a5_1,a5_2,a5_3,a5_4, type = "text")

#####################################################################
# Table F1: Trust in Politics/Satisfaction with Democracy
###################################################################### 
# Trust in Politics
# Column 1
f1_1 <- lm( trust_pols ~ inc_unfair + top_inc_unfair + bttm_inc_unfair + 
              college + female + age + income + union + unemployed + 
              citizen + religion + urban + cntry,
            data = dat,
            weights = dweight) 
f1_1 <- robustify(f1_1 , cluster = cntry)

# Column 2
f1_2 <- lm( trust_pols ~ job_self_unfair + job_other_unfair +
              college + female + age + income + union + unemployed + 
              citizen + religion + urban + cntry,
            data = dat,
            weights = dweight) 
f1_2 <- robustify(f1_2 , cluster = cntry)

# Satisfaction with democracy
# Column 1
f1_3 <- lm( sat_democracy ~ inc_unfair + top_inc_unfair + bttm_inc_unfair + 
              college + female + age + income + union + unemployed + 
              citizen + religion + urban + cntry,
            data = dat,
            weights = dweight) 
f1_3 <- robustify(f1_3 , cluster = cntry)

# Column 2
f1_4 <- lm( sat_democracy ~ job_self_unfair + job_other_unfair +
              college + female + age + income + union + unemployed + 
              citizen + religion + urban + cntry,
            data = dat,
            weights = dweight) 
f1_4 <- robustify(f1_4 , cluster = cntry)

stargazer(f1_1,f1_2, f1_3, f1_4, type = "text")

#######################################
# Appendix G: Eastern Europe
#######################################
## Eastern Europe (Hungray and Poland)
dat_east <- read.csv("ESS_cleaned_eastern.csv")

# Hungary
# Column 1
g1_1 <- glm( far_right ~ inc_unfair + top_inc_unfair + bttm_inc_unfair + 
               college + female + age + income + union + unemployed + 
               citizen + religion + urban ,
             data = dat_east %>% filter(cntry == "Hungary"),
             weights = dweight, family=binomial(link=logit)) 
g1_1 <- robustify(g1_1)

# Column 2
g1_2 <- glm( far_right ~ job_self_unfair + job_other_unfair +
               college + female + age + income + union + unemployed + 
               citizen + religion + urban,
             data = dat_east %>% filter(cntry == "Hungary"),
             weights = dweight, family=binomial(link=logit)) 
g1_2 <- robustify(g1_2)

# Poland
# Column 3
g1_3 <- glm( far_right ~ inc_unfair + top_inc_unfair + bttm_inc_unfair + 
               college + female + age + income + union + unemployed + 
               citizen + religion + urban ,
             data = dat_east %>% filter(cntry == "Poland"),
             weights = dweight, family=binomial(link=logit)) 
g1_3 <- robustify(g1_3)

# Column 4 
g1_4 <- glm( far_right ~ job_self_unfair + job_other_unfair +
               college + female + age + income + union + unemployed + 
               citizen + religion + urban,
             data = dat_east %>% filter(cntry == "Poland"),
             weights = dweight, family=binomial(link=logit)) 
g1_4 <- robustify(g1_4)

# Combined
stargazer(g1_1, g1_2, g1_3,g1_4, type = "text")


